package com.helper.dream.a361phonehelper.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.adapter.TuijianJingpinAdapter;
import com.helper.dream.a361phonehelper.bean.TuijianJingpinEntity;
import com.helper.dream.a361phonehelper.inter.ITuijianJingpinInterface;
import com.helper.dream.a361phonehelper.utils.ApiManager;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LamYu on 16/12/19/0019.
 */

public class TuijianJingpinFragment extends Fragment {
    TuijianJingpinAdapter adapter;
    TuijianJingpinEntity data;
    RecyclerView rv;
    View view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_tuijian_jingpin,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rv= (RecyclerView) view.findViewById(R.id.rv_tj_jp);
        getData();
    }

    private void getData() {
        Retrofit retrofit=new Retrofit.Builder().baseUrl(ApiManager.TjJingpinBase).addConverterFactory(GsonConverterFactory.create()).build();
        ITuijianJingpinInterface tuijianJingpinInterface=retrofit.create(ITuijianJingpinInterface.class);
        Call<TuijianJingpinEntity> call=tuijianJingpinInterface.getTjJpEntity();
        call.enqueue(new Callback<TuijianJingpinEntity>() {
            @Override
            public void onResponse(Call<TuijianJingpinEntity> call, Response<TuijianJingpinEntity> response) {
                data=response.body();
                adapter = new TuijianJingpinAdapter(getActivity(),data);
                rv.setLayoutManager(new LinearLayoutManager(getActivity()));
                rv.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<TuijianJingpinEntity> call, Throwable t) {

            }
        });
    }
}
